import {NetConfig} from "../config/config";

const API_URL = NetConfig.baseUrl;

const get = async (endpoint: string, queryParams:any = {}) => {
    const query = Object.keys(queryParams)
        .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`)
        .join('&');
    const url = `${API_URL}/${endpoint}?${query}`;

    try {
        const response = await fetch(url);
        return await response.json();
    } catch (error) {
        console.log(error);
    }
};

const post = async (endpoint:string, data = {}) => {
    const url = `${API_URL}/${endpoint}`;

    try {
        const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
        });
        return await response.json();
    } catch (error) {
        console.log(error);
    }
};
export {get,post}